package com.moyun.common.generator;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.moyun.common.utils.YmlUtils;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DBDocumentGenerator
 * @Description 数据库文档的自动生成
 * @Author yangboxuan
 * @Date 2021/5/27 23:10
 */
public class DBDocumentGenerator {

    /**
     * 依赖的jar 包  screw 和 HikariCP
     * 如果需要灵活配置,自行更改即可
     **/
    @Test
    public void generateDBDocumentation() {

        // 1.设置数据源
        HikariConfig hikariConfig = new HikariConfig();
//        hikariConfig.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//        hikariConfig.setJdbcUrl("jdbc:sqlserver://119.3.237.220:1433;DatabaseName=om-app");
//        hikariConfig.setUsername("sa");
//        hikariConfig.setPassword("Moyun2021");

//        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        hikariConfig.setJdbcUrl("jdbc:mysql://xxx:xx/xx?useUnicode=true&characterEncoding=utf-8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8");
//        hikariConfig.setUsername("root");
//        hikariConfig.setPassword("xxx");
        Map<String, String> ymlMap = YmlUtils.getYmlByFileName("classpath:application-dev.yml", "spring","datasource","druid");
        String driverClassName = ymlMap.get("spring.datasource.druid.driver-class-name");
        String jdbcUrl = ymlMap.get("spring.datasource.druid.url");
        String username = ymlMap.get("spring.datasource.druid.username");
        String password = ymlMap.get("spring.datasource.druid.password");
        hikariConfig.setDriverClassName(driverClassName);
        hikariConfig.setJdbcUrl(jdbcUrl);
        hikariConfig.setUsername(username);
        hikariConfig.setPassword(password);
        hikariConfig.addDataSourceProperty("useInformationSchema", "true");
        hikariConfig.setMinimumIdle(2);
        hikariConfig.setMaximumPoolSize(5);
        DataSource dataSource = new HikariDataSource(hikariConfig);

        // 生成文件配置
        EngineConfig engineConfig = EngineConfig.builder()
                // 生成文件路径
                .fileOutputDir("F:\\")
                // 打开目录
                .openOutputDir(false)
                // 文件类型
                .fileType(EngineFileType.WORD)
                // 生成模板实现
                .produceType(EngineTemplateType.freemarker).build();

        // 生成文档配置 (包含以下自定义版本号、描述等配置连接)
        Configuration config = Configuration.builder()
                .version("1.0.0")
                .description("数据库文档说明")
                .dataSource(dataSource)
                .engineConfig(engineConfig)
                .produceConfig(getProcessConfig())
                .build();

        // 执行生成
        new DocumentationExecute(config).execute();
    }

    /**
     * @Author yangboxuan
     * @Description 配置想要生成的表+配置想要忽略的表
     * @Date 2021/4/8 18:21
     * @Param []
     * @return cn.smallbun.screw.core.process.ProcessConfig
     **/
    public static ProcessConfig getProcessConfig(){
        // 忽略表名
        List<String> ignoreTableName = Arrays.asList("");
        // 忽略表前缀，如忽略a开头的数据库表
        List<String> ignorePrefix = Arrays.asList("");
        // 忽略表后缀
        List<String> ignoreSuffix = Arrays.asList("");

        return ProcessConfig.builder()
                //根据名称指定表生成
//                .designatedTableName(Arrays.asList("errlog","mp_appconfig","mp_applist","mp_apptype","mp_appversion","mp_attachment","mp_dept","mp_file","mp_form","mp_form_area","mp_form_category","mp_form_child","mp_form_field","mp_form_group","mp_form_progress","mp_i18n","mp_lock","mp_log","mp_message","mp_message_type","mp_news","mp_notice_list","mp_notice_type","mp_page","mp_page_role","mp_perssion","mp_personal_affairs","mp_picman","mp_role","mp_role_menu_map","mp_role_permission_map","mp_security","mp_user","mp_user_app_commony","mp_user_copy","mp_user_role_map","oauth_token","oauth_token_key","sso"))
//                .designatedTableName(Arrays.asList("department","sys_oper_log","sys_privilege","sys_role","sys_role_privilege","sys_user","sys_user_role"))
                .designatedTableName(Arrays.asList("log_exc"))

//                //根据表前缀生成
//                .designatedTablePrefix(new ArrayList<>())
//                //根据表后缀生成
//                .designatedTableSuffix(new ArrayList<>())
                //忽略表名
                .ignoreTableName(ignoreTableName)
                //忽略表前缀
                .ignoreTablePrefix(ignorePrefix)
                //忽略表后缀
                .ignoreTableSuffix(ignoreSuffix).build();
    }

}
